package com.ganee.agora.view

import android.os.Bundle
import android.view.View
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.ganee.agora.R
import com.ganee.agora.adapter.RepeatListAdapter
import com.ganee.agora.model.RepeatListEntity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.ganee.agora.event.RepeatEvent
import com.ganee.agora.viewmodel.CategoryViewModel
import com.ganee.agora.widget.itemDecoration.MyDecoration
import com.ganee.baselib.arouter.Constance
import com.ganee.baselib.mvvmlibrary.base.BaseActivity
import com.google.gson.Gson
import kotlinx.android.synthetic.main.activity_category.*
import kotlinx.android.synthetic.main.toolbar_with_title.*
import org.greenrobot.eventbus.EventBus

@Route(path = Constance.ACTIVITY_URL_CATEGORY)
class CategoryActivity : BaseActivity<CategoryViewModel>(),View.OnClickListener{

    private val dataList:MutableList<RepeatListEntity> = mutableListOf()
    private val chooseList:MutableList<RepeatListEntity> = mutableListOf()
    private var mAdapter:RepeatListAdapter? = null

    @JvmField
    @Autowired(name = "chooseList")
    var choose:String? = null

    override fun getViewModel(): CategoryViewModel {
        return ViewModelProviders.of(this@CategoryActivity).get(CategoryViewModel::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_category)
        ARouter.getInstance().inject(this)
        initToolbar()
        initData()
        initViewAndAdapter()
    }

    private fun initToolbar() {
        toolbarCancel.setOnClickListener(this)
        footer_tv_confirm.setOnClickListener(this)
        toolbarTitle.text = "Add some tags"
    }

    private fun initData() {
        dataList.add(RepeatListEntity("music",false))
        dataList.add(RepeatListEntity("drama",false))
        dataList.add(RepeatListEntity("dance",false))
        dataList.add(RepeatListEntity("films",false))
        dataList.add(RepeatListEntity("animals",false))
        dataList.add(RepeatListEntity("exercise",false))
        dataList.add(RepeatListEntity("yoga",false))
        dataList.add(RepeatListEntity("meditation",false))
        dataList.add(RepeatListEntity("nature",false))
        dataList.add(RepeatListEntity("food",false))
        dataList.add(RepeatListEntity("cultural events",false))
        dataList.add(RepeatListEntity("peer support",false))
        dataList.add(RepeatListEntity("spirituality",false))
        dataList.add(RepeatListEntity("visual arts",false))
        dataList.add(RepeatListEntity("museums",false))

//        if (choose != null){
//            chooseList.addAll(Gson().fromJson(choose, object : TypeToken<List<RepeatListEntity>>() {}.type))
//        }
//        for(entity in dataList){
//            entity.check = false
//            for (choose in chooseList){
//                if (entity.title == choose.title){
//                    entity.check = true
//                }
//            }
//        }
    }

    private fun initViewAndAdapter() {
        categoryRecyclerView.setHasFixedSize(true)
        categoryRecyclerView.layoutManager = LinearLayoutManager(this)
        categoryRecyclerView.addItemDecoration(MyDecoration())
        categoryRecyclerView.isNestedScrollingEnabled = false
        mAdapter = RepeatListAdapter(dataList)

        categoryRecyclerView.addOnItemTouchListener(object : OnItemClickListener() {

            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                val entity = adapter.getItem(position) as RepeatListEntity
                if (!entity!!.check) {
                    entity!!.check = true
                    chooseList.add(entity)
                    if (chooseList.size > 3){
                        chooseList[0].check = false
                        chooseList.remove(chooseList[0])
                    }
                } else {
                    entity!!.check = false
                    for (i in chooseList.size - 1 downTo 0) {
                        if (chooseList[i].title == entity!!.title) {
                            chooseList.remove(chooseList[i])
                        }
                    }
                }
                mAdapter!!.notifyDataSetChanged()
            }

        })
        categoryRecyclerView.adapter = mAdapter
    }

    override fun onClick(v: View?) {
        when(v!!.id){
            R.id.toolbarCancel -> {
                finish()
            }
            R.id.footer_tv_confirm -> {
                EventBus.getDefault().post(RepeatEvent("Category", Gson().toJson(chooseList)))
                finish()
            }
        }
    }
}
